package com.community.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.community.pojo.Collect;
import com.community.pojo.Product;
import com.community.service.ICollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author heyuhang
 * @create 2024/6/19
 */
@CrossOrigin
@Controller
@RequestMapping("/community/collect")
public class CollectController {

    @Autowired
    private ICollectService collectService;

    /**
     * 查询所有收藏的
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public List<Collect> collectList() {
        QueryWrapper<Collect> wrapper = new QueryWrapper<>();
        wrapper.eq("is_collect",1);
        return collectService.list(wrapper);
    }

    /**
     * 收藏成功
     * @param product
     * @return
     */
    @RequestMapping("/collectSuccessful")
    @ResponseBody
    public boolean collect(@RequestBody Product product) {
        System.out.println("product++++"+product);
        Collect collect = new Collect();
        collect.setName(product.getName());
        collect.setPrice(product.getPrice());
        collect.setImgLink(product.getImgLink());
        boolean save = collectService.save(collect);
        if(save){
            collect.setIsCollect(1);
            return collectService.updateById(collect);
        }
        return true;
    }
}
